package com.kaifamiao.trace;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet( "/user/welcome/page" )
public class UserWelcomePageServlet extends HttpServlet {

    @Override
    protected void doGet( HttpServletRequest request , HttpServletResponse response )
            throws ServletException, IOException {

        HttpSession session = request.getSession();

        String username = "" ;
        User user = (User)session.getAttribute( "user" );
        if( user != null ) {
            username = user.getUsername();
        }

        response.setCharacterEncoding( "UTF-8" );

        response.setContentType( "text/html;charset=UTF-8" );

        PrintWriter out = response.getWriter();

        out.println( "<!DOCTYPE html>" );
        out.println( "<html>" );
        out.println( "<head>" );
        out.println( "<meta charset='UTF-8'>" );
        out.println( "<title>欢迎</title>" );
        out.println( "</head>" );
        out.println( "<body>" );
        out.println( "<h3>欢迎" + username + "</h3>" );
        if( user == null ) {
            out.println( "<a href='/user/sign/in/page'>登录</a>" );
        } else {
            out.println( "<a href='/user/sign/out/action'>退出</a>" );
        }
        out.println( "</body>" );
        out.println( "</html>" );

    }

}
